package com.remainder.resource.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.remainder.common.core.enums.YN;
import com.remainder.common.core.exception.BusinessException;
import com.remainder.resource.mapper.UserMapper;
import com.remainder.resource.service.IUserService;
import com.remainder.sdk.export.service.BatchExport;
import com.remainder.system.api.domain.User;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户Service业务层处理
 *
 * @author remainder
 * @date 2023-05-27
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {
    /**
     * 手机号前缀
     */
    private static String[] telFirst =
        "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
    @Resource
    private UserMapper userMapper;
    @Autowired
    private BatchExport batchExportRedis;

    /**
     * 返回手机号码
     */
    private static String getTel() {
        int index = getNum(0, telFirst.length - 1);
        String first = telFirst[index];
        String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
        String third = String.valueOf(getNum(1, 9100) + 10000).substring(1);
        return first + second + third;
    }

    public static int getNum(int start, int end) {
        return (int)(Math.random() * (end - start + 1) + start);
    }

    /**
     * 查询用户
     *
     * @param id
     *            用户主键
     * @return 用户
     */
    @Override
    public User selectUserById(Long id) {
        return userMapper.selectUserById(id);
    }

    /**
     * 查询用户列表
     *
     * @param user
     *            用户
     * @return 用户
     */
    @Override
    public List<User> selectUserList(User user) {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户
     *
     * @param user
     *            用户
     * @return 结果
     */
    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    /**
     * 批量新增用户
     *
     * @param user
     *            用户
     * @return 结果
     */
    @Override
    public int batchInsertUser(User user) {
        // 批量生成手机号
        Integer batchAmount = user.getBatchAmount();
        if (Objects.isNull(batchAmount) && batchAmount != 0) {
            throw new BusinessException("请求参数异常，请检查后重试");
        }
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < batchAmount; i++) {
            User temp = new User();
            String iphone = getTel();
            temp.setNickName(iphone);
            temp.setIphone(iphone);
            temp.setPayPassword("1234");
            temp.setLoginPassword("123456");
            temp.setAccountStatus(YN.YES.getCode().toString());
            temp.setRegisterDate(new Date());
            userList.add(temp);
        }
        return userMapper.batchInsertUser(userList);
    }

    /**
     * 修改用户
     *
     * @param user
     *            用户
     * @return 结果
     */
    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户
     *
     * @param ids
     *            需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids) {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除用户信息
     *
     * @param id
     *            用户主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id) {
        return userMapper.deleteUserById(id);
    }
}
